<template>
    <div class="signup-page mx-auto p-3 w-330">
        <h5 class="my-4 text-center">注册者也账户</h5>
        <ValidateForm @form-submit="onFormSubmit">
            <div class="mb-3">
                <label class="form-label">邮箱地址</label>
                <validate-input :rules="emailRules" v-model="formData.email" placeholder="请输入邮箱地址" type="text"
                    ref="inputRef" />
            </div>
            <div class="mb-3">
                <label class="form-label">昵称</label>
                <validate-input :rules="nameRules" v-model="formData.nickName" placeholder="请输入昵称" type="text"
                    ref="inputRef" />
            </div>
            <div class="mb-3">
                <label class="form-label">密码</label>
                <validate-input :rules="passwordRules" v-model="formData.password" placeholder="请输入密码" type="password"
                    ref="inputRef" />
            </div>
            <div class="mb-3">
                <label class="form-label">重复密码</label>
                <validate-input :rules="repeatPasswordRules" v-model="formData.repeatPassword" placeholder="请确认密码"
                    type="password" ref="inputRef" />
            </div>
            <template #submit>
                <button class="btn btn-primary">注册新用户</button>
            </template>
        </ValidateForm>
    </div>
</template>
<script lang="ts">
import { defineComponent, reactive } from 'vue'
import ValidateForm from '@/components/ValidateForm.vue'
import ValidateInput, { RulesProp } from '@/components/ValidateInput.vue'
import CreateMessage from '@/components/CreateMessage'
import axios from 'axios'
import { useRouter } from 'vue-router'
export default defineComponent({
    name: 'Signup',
    components: {
        ValidateForm,
        ValidateInput
    },
    setup() {
        const router = useRouter()
        const formData = reactive({
            email: '',
            nickName: '',
            password: '',
            repeatPassword: ''
        })
        const emailRules: RulesProp = [
            { type: 'required', message: '电子邮箱地址不能为空' },
            { type: 'email', message: '请输入正确的电子邮箱格式' }
        ]
        const passwordRules: RulesProp = [
            { type: 'required', message: '密码不能为空' },
            { type: 'range', min: { length: 6, message: '长度不得小于6位' } }
        ]
        const nameRules: RulesProp = [{ type: 'required', message: '昵称不能为空' }]
        const repeatPasswordRules: RulesProp = [
            { type: 'required', message: '密码不能为空' },
            {
                type: 'custom',
                validator: () => {
                    return formData.password === formData.repeatPassword
                }
            }
        ]
        const onFormSubmit = (result: boolean) => {
            console.log('result', result)
            if (result) {
                const payload = {
                    email: formData.email,
                    nickName: formData.nickName,
                    password: formData.password
                }
                axios.post('/users', payload).then(() => {
                    CreateMessage('注册成功，正在跳转登录页面', 'success')
                    setTimeout(() => {
                        router.push('/login')
                    }, 2000)
                }).catch(e => {
                    console.log(e)
                })
            }
        }
        return {
            formData,
            emailRules,
            passwordRules,
            nameRules,
            repeatPasswordRules,
            onFormSubmit
        }
    }
})
</script>
<style>
.w-330 {
    max-width: 330px;
}
</style>
